/**
 * 
 */
package com.gmail.sabuj.career.qna.core.expression;

import com.gmail.sabuj.career.qna.core.fsm.Action;
import com.gmail.sabuj.career.qna.core.fsm.FiniteStateMachine;
import com.gmail.sabuj.career.qna.core.fsm.State;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class ExpressionStateMachine extends FiniteStateMachine<String>{
	
	public static final State<String> START_STATE = new State<String>("START");
	public static final State<String> ERROR_STATE = new State<String>("ERROR");
	public static final State<String> SUCCESS_STATE = new State<String>("SUCCESS");
	
	static{
		START_STATE.setBefore(null);
		SUCCESS_STATE.setAfter(null);
	}
	
	@Override
	public void buildMachine(String expression) {
		buildMachine(expression, false);
	}
	
	@Override
	public void buildMachine(String expression, boolean caseInSensitive) {
		if(expression != null && expression.trim().length() > 0)
			expression = expression.trim();
		else
			throw new IllegalArgumentException(expression);
		if(caseInSensitive)
			expression = expression.toLowerCase();
		
		char[] chars = expression.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			char ch = chars[i];
			
			// if the char from a-to-z
			if((ch >= 'a' && ch <= 'z')
					|| (ch >= 'A' && ch <= 'Z')){
				State<String> state = null;
				if(stateCache.containsKey(State.STATE_NAME_PREFIX + ch)){
					state = stateCache.get(State.STATE_NAME_PREFIX + ch);
				} else {
					state = new State<String>(State.STATE_NAME_PREFIX + ch);
					stateCache.put(State.STATE_NAME_PREFIX + ch, state);
				}
				
				if(state != null){
					addState(state);
				}
				
			} else {
				if(i > 0){
					if(ch == '*'){
						char prev = chars[i-1];
					}
				}
			}
			
			
		}
		
	}
	
	
}